package com.atguigu.Serivce.Impl;

import com.atguigu.GlobalExceptionHandler.CustomException;
import com.atguigu.Serivce.SetmealDishService;
import com.atguigu.Serivce.SetmealService;
import com.atguigu.Utils.R;
import com.atguigu.mapper.CategoryMapper;
import com.atguigu.mapper.SetmealMapper;
import com.atguigu.pojo.Category;
import com.atguigu.pojo.Dto.SetmealDto;
import com.atguigu.pojo.Setmeal;
import com.atguigu.pojo.SetmealDish;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

   @Autowired
   private SetmealDishService setmealDishService;

   @Autowired
   private CategoryMapper  categoryMapper;

   @Value("${File.basePath}")//图片文件目录
    private String basePath;


    @Override
    @Transactional
    public R addSetmeal(SetmealDto setmealDto) {
        setmealMapper.insert(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId().toString());
        });


        setmealDishService.saveBatch(setmealDishes);

    return R.success("成功添加");
    }

    @Override
    public R page(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealMapper.selectPage(pageInfo, wrapper);

        //除了records数据，其他数据拷贝到dtopage，因为records数据要经过一些调整
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");


        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> dtoList =new ArrayList<>();//要将dtoList经过赋值后赋值给dtoPage

        records.forEach(setmeal -> {
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            if (category != null) {
               setmealDto.setCategoryName(category.getName());
            }
            dtoList.add(setmealDto);
        });

        dtoPage.setRecords(dtoList);
        return R.success(dtoPage);
    }


    @Override
    @Transactional
    public R remove(Long[] ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper =new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SetmealDish> queryDishWrapper =new LambdaQueryWrapper<>();


        //验证套餐参是否启售
        queryWrapper.in(Setmeal::getId,ids);
        List<Setmeal> setmeals = setmealMapper.selectList(queryWrapper);
        for (Setmeal setmeal : setmeals) {
            if (setmeal.getStatus() == 1) {
                throw new CustomException(setmeal.getName()+"启售中");
            }
        }


        //删除套餐，如果套餐有图片也一并删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            String image = setmeal.getImage();
            File file = new File(basePath + image);
            if (file.exists()) {
                file.delete();
            }
        }

        //删除套餐，也需要删除与菜品有关的记录
        queryDishWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryDishWrapper);

        //删除套餐
        setmealMapper.delete(queryWrapper);

        return R.success("删除成功");
    }


    @Override
    public R updateSetmealState(int status, Long[] ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);

        setmealMapper.update(setmeal,queryWrapper);
        return R.success("修改成功");
    }

    @Override
    public R list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());

        List<Setmeal> setmeals = setmealMapper.selectList(queryWrapper);
        return R.success(setmeals);
    }
}
